package com.one.common.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.one.common.CommonApp;
import com.one.common.manager.ActivityPageManager;

import java.util.UUID;


public class SystemUtils {
    /**
     * killing app from process level
     *
     * @param context
     */
    public static void killProcess(Context context) {
        /**
         //调用android.os.Process.killProcess(android.os.Process.myPid());和System.exit(0);
         //系统判定程序异常关闭，会尝试重新启动
         //如果退出进程前先关闭所有的activity
         //这样系统就不会重新启动入口activity
         //不会出现多次报错的问题
         */

        ActivityPageManager.getAppManager().finishAllActivity();

        android.os.Process.killProcess(android.os.Process.myPid());

        System.exit(0);
    }

    public static void killProcess() {
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    public static String getIMSI() {
        TelephonyManager telephonyManager = (TelephonyManager) CommonApp.getInstance().getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String IMSI = telephonyManager.getSubscriberId();
        return IMSI;
    }

    @SuppressLint("MissingPermission")
    public static String getDeviceID(Context context) {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = "";
            if (android.os.Build.VERSION.SDK_INT >= 28) {
                final String tmDevice, tmSerial, androidId;
                tmDevice = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
                tmSerial = "ycpandroid";
                androidId = "ycp_hyy";
                UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
                deviceId = deviceUuid.toString();
                return deviceId;
            }else {
                final String tmDevice, tmSerial, androidId;
                tmDevice = tm.getDeviceId() + "";
                tmSerial = tm.getSimSerialNumber() + "";
                // androidid 会变化 所以此处写死
                androidId = "ycp_hyy";
                UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
                deviceId = deviceUuid.toString();
                return deviceId;
            }
    }

    private static String bytes2Hex(byte[] bytes) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bytes.length; i++) {
            tmp = (Integer.toHexString(bytes[i] & 0xFF));
            if (tmp.length() == 1)
                des += "0";
            des += tmp;
        }
        return des;
    }

    /**
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        PackageInfo packInfo = getPackageInfo(context);
        if (packInfo != null) {
            return packInfo.versionCode;
        } else {
            return -1;
        }
    }

    /**
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageInfo packInfo = getPackageInfo(context);
        if (packInfo != null) {
            return packInfo.versionName;
        } else {
            return "未知";
        }
    }

    public static PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (Exception e) {
            return null;
        }
    }


    public static boolean isPackageExisted(String targetPackage) {
        PackageManager pm = CommonApp.getInstance().getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
        return true;
    }

    /**
     * 调用拨号界面
     *
     * @param phone 电话号码
     */
    public static void call(Context context, String phone, boolean isAuth) {
        // && !CMemoryData.getUserState().isOWTB()
        if (!isAuth) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
                if (phone.contains("-")) {
                    phone = phone.replace("-", "");
                }
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
        }
    }

    public static void call(Context context, String phone) {
        call(context, phone, true);
    }
}
